#!/usr/bin/env python
# -*- coding: utf-8 -*-

# 字符串
a = 250
b = "250"
print unicode("a的对象类型为:", "utf-8") # 解决中午输出乱码问题
print type(a) # 查看对象类型
print unicode("b的对象类型为:", "utf-8")
print type(b)

c = "Python"
d = 2016
print c + str(d) # 把int类型强转成String后再与String变量进行拼接打印
print c + repr(d) # repr()和 str的区别，一个最简单的区别，repr 是函数，str 是跟 int 一样，一种对象类型

# 趣味小对话练习
name = raw_input(unicode("请输入您的名字:\n", "utf-8"))
print unicode("您好" + name + "\n我是Mark,很高兴认识你!", "utf-8")
age = raw_input(unicode("请输入您的年龄:\n", "utf-8"))
if int(age) < 28:
    print unicode("我今年已经28岁了,请叫一声哥哥来听听!!!", "utf-8")
else:
    print unicode("原来你这么老啦,请受小弟一拜!!!", "utf-8")

# 索引和切片
lang = "I want to study Python!!!"
print lang[4] # 打印出来的结果是“n”,因为是从0开始计算
print lang.index("t") # 打印出的结果是5,这是第一个“t”的索引
print lang[2:15] # 打印结果:“want to study” 打印的是字符串索引为2到15的字符串
print lang[2:] # 打印结果:“want to study Python!!!” 打印的是字符串索引为2到末尾的字符串
print lang[:] # 打印结果:“I want to study Python!!!” 打印的是字符串索引为0到末尾的字符串

# 字符串操作
# 判断在lang中是否存在P这个字符
if "P" in lang:
    print lang.index("P")
else:
    print unicode("在lang中没有P", "utf-8")

# 两个字符串的比较,cmp也可以用于整数的比较
lang2 = "I don't want to study Python!!!"
if cmp(lang, lang2) == 0:
    print unicode("lang和lang2的字符串相同", "utf-8")
else:
    print unicode("lang和lang2的字符串不相同", "utf-8")
    print cmp(lang,lang2)

# 查看字符串的长度
print len(lang)

# 字符串的格式化输出（接入的类型是string时使用%s,接入int时使用%d,接入浮点数用%f）
print "I love you, %s" % unicode("傻妞", "utf-8") # 输出结果:I love you, 傻妞
print unicode("我今年%d岁", "utf-8") % 27 # 输出结果:我今年27岁
print unicode("你好,我叫%s\n我今年%d岁了\n我已经知道圆周率是前三位是:%.2f", "utf-8") % ("Mark", 27, 3.14) # 多种类型组合

# 新方法（将{}作为占位符,{}里面的可以理解为一个需要被赋值位置的key）
print "I am {myName}, I love you, {name}".format(myName = "Mark", name = unicode("傻妞", "utf-8").encode("utf-8"))

# 分割字符串
print lang.split(" ") # 结果['I', 'want', 'to', 'study', 'Python!!!'] 就是以" "为标志来作为分割字符串的key
print type(lang.split(" ")) # list类型
# 把分割的字符串拼接起来
lang_sp = lang.split(" ")
lang_join = " ".join(lang_sp)
print "{tips}{old}\n{newTips}{new}".format(tips = unicode("分割后的lang:", "utf-8").encode("utf-8"), old = lang_sp, newTips = unicode("拼接回来的lang:", "utf-8").encode("utf-8"), new = lang_join)

# 去空格
lang3 = " " + lang + " "
print unicode("加完空格后的lang:\n", "utf-8").encode("utf-8") + lang3
print unicode("去掉两边空格后的lang:\n", "utf-8").encode("utf-8") + lang3.strip()
print unicode("去掉左侧空格后的lang:\n", "utf-8").encode("utf-8") + lang3.lstrip()
print unicode("去掉右侧空格后的lang:\n", "utf-8").encode("utf-8") + lang3.rstrip()

# 字母的大小写转换
print unicode("全部转成大写的lang:\n", "utf-8").encode("utf-8") + lang.upper()
print unicode("全部转成小写的lang:\n", "utf-8").encode("utf-8") + lang.lower()
